<?php

namespace app\admin\controller;

use app\admin\common\Base;
use app\admin\model\CompanyHistory;

/**
 * 公司信息控制器
 * Class Company
 * @package app\admin\controller
 */
class Company extends Base {
    /**
     * 公司信息
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index(){
        $info = db('company')->where('id',1)->find();
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function info(){
        $data = input('post.');
        $data['update_time'] = time();
        $result = db('company')
            ->where('id',1)
            ->data($data)
            ->update();
        if ($result){
            insert_system_log('修改公司信息');
            return json(['code' => 'suc', 'msg' => '修改成功']);
        }else {
            return json(['code' => 'err', 'msg' => '系统繁忙，请重试']);
        }
    }

    /**
     * 职位列表
     * @param int $page
     * @param int $limit
     * @return mixed|\think\response\Json
     */
    public function position($page = 1, $limit = 10){
        if (is_post()){
            $where[] = ['is_del','=',0];
            $data = $this->getListData('position',$where,'sort','asc',$page,$limit);
            return json(['code'=>'suc','msg'=>'数据请求成功','count'=>$data['count'],'data'=>$data['list']]);
        }
        return $this->fetch('company/position/list');
    }

    /**
     * 新增职位
     * @return mixed|\think\response\Json
     */
    public function addPosition(){
        if (is_post()){
            $data = input('post.');
            if (empty($data['name'])){
                return json(['code' => 'err','msg' => '请输入职位名称']);
            }
            $data['status'] = empty($data['status']) ? 0 : 1;
            $data['create_time'] = time();
            $data['update_time'] = time();
            $repeat = db('position')
                ->where('name',$data['name'])
                ->where('is_del',0)
                ->find();
            if ($repeat){
                return json(['code' => 'err','msg' => '职位'. $data['name'] . '已存在']);
            }
            $result = db('position')->data($data)->insertGetId($data);
            if ($result){
                insert_system_log("添加职位：" . $result);
                return json(['code' => 'suc','msg'=>'添加成功']);
            }
            return json(['code' => 'err','msg'=>'系统繁忙，请重试']);
        }
        return $this->fetch('company/position/add');
    }

    /**
     * 修改职位
     * @param $id
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editPosition($id){
        if (is_post()){
            $data = input('post.');
            if (empty($data['name'])){
                return json(['code' => 'err','msg' => '请输入职位名称']);
            }
            $data['status'] = empty($data['status']) ? 0 : 1;
            $data['create_time'] = time();
            $data['update_time'] = time();
            $repeat = db('position')
                ->where('name',$data['name'])
                ->where('id','<>',$id)
                ->where('is_del',0)
                ->find();
            if ($repeat){
                return json(['code' => 'err','msg' => '职位'. $data['name'] . '已存在']);
            }
            $result = db('position')
                ->where('id',$id)
                ->data($data)
                ->update($data);
            if ($result){
                insert_system_log("修改职位：" . $id);
                return json(['code' => 'suc','msg'=>'修改成功']);
            }
            return json(['code' => 'err','msg'=>'系统繁忙，请重试']);
        }
        $info = db('position')
            ->where('id',$id)
            ->find();
        $this->assign('info',$info);
        return $this->fetch('company/position/edit');
    }

    /**
     * 删除职位
     * @param $id
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delPosition($id){
        if (is_post()){
            if ($this->delRecord('position',$id,'删除职位：')){
                return json(['code' => 'suc','msg' => '已删除']);
            }
            return json(['code' => 'err','msg' => '系统繁忙，请重试']);
        }
    }

    /**
     * 修改职位状态
     * @param $id
     * @param $type
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function changePositionStatus($id,$type){
        if (is_post()){
            if ($this->changeRecordStatus('position',$id,1,$type)){
                if ($type == 1){
                    $msg = '已显示';
                    $log_msg = '显示职位：';
                } else {
                    $msg = '已隐藏';
                    $log_msg = '隐藏职位：';
                }
                insert_system_log($log_msg);
                return json(['code' => 'suc','msg' => $msg]);
            }
            return json(['code' => 'err','msg' => '系统繁忙，请重试']);
        }
    }

    /**
     * 公司图片
     * @param int $page
     * @param int $limit
     * @param string $keyword
     * @param string $category
     * @throws
     * @return mixed|\think\response\Json
     */
    public function picture($page = 1, $limit = 10, $keyword = '', $category = ''){
        if (is_post()){
            $where[] = ['is_del','=',0];
            if(!empty($keyword)){
                $where[] = ['name','like','%' . $keyword . '%'];
            }

            if (!empty($category)){
                $where[] = ['type','=',$category];
            }

            $data = $this->getListData('company_picture',$where,'sort','asc',$page,$limit);

            $type = db('category_company_picture')
                ->where('is_del',0)
                ->where('status',1)
                ->field('id,name')
                ->select();
            $type_dic = to_dict($type);

            foreach ($data['list'] as $k => $v){
                $data['list'][$k]['type'] = $type_dic[$v['type']];
            }

            return json(['code'=>'suc','msg'=>'数据请求成功','count'=>$data['count'],'data'=>$data['list'],'current_page'=>$page]);
        }
        $this->assign('type',$this->getCompanyPicCategory());
        return $this->fetch('company/picture/list');
    }

    /**
     * 新增公司图片
     * @return mixed|\think\response\Json
     */
    public function addPicture(){
        if (is_post()){
            $data = input('post.');
            $data['status'] = empty($data['status']) ? 0 : 1;
            $data['create_time'] = time();
            $data['update_time'] = time();
            $result = db('company_picture')->insertGetId($data);
            if ($result){
                insert_system_log('添加公司图片：' . $result);
                return json(['code' => 'suc', 'msg' => '添加成功']);
            }else {
                return json(['code' => 'err', 'msg' => '系统繁忙，请重试']);
            }
        }
        $this->assign('type',$this->getCompanyPicCategory());
        return $this->fetch('company/picture/add');
    }

    /**
     * 修改公司图片
     * @param $id
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editPicture($id){
        if (is_post()){
            $data = input('post.');
            $data['status'] = empty($data['status']) ? 0 : 1;
            $data['update_time'] = time();
            $result = db('company_picture')->where('id',$id)->data($data)->update($data);
            if ($result){
                insert_system_log('修改公司图片：' . $id);
                return json(['code' => 'suc', 'msg' => '修改成功']);
            }else {
                return json(['code' => 'err', 'msg' => '系统繁忙，请重试']);
            }
        }
        $info = db('company_picture')
            ->where('id',$id)
            ->find();
        $this->assign('info',$info);
        $this->assign('type',$this->getCompanyPicCategory());
        return $this->fetch('company/picture/edit');
    }

    /**
     * 删除公司图片
     * @param $id
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delPicture($id){
        if (is_post()){
            if ($this->delRecord('company_picture',$id,'删除公司图片：')){
                return json(['code' => 'suc','msg' => '已删除']);
            }
            return json(['code' => 'err','msg' => '系统繁忙，请重试']);
        }
    }

    /**
     * 修改公司图片状态
     * @param int $type 公司图片分类ID
     * @param int $id 公司图片记录id
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function changePictureStatus($type,$id){
        if (is_post()){
            if ($this->changeRecordStatus('company_picture',$id,1,$type)){
                if ($type == 1){
                    $msg = '已显示';
                    $log_msg = '显示公司图片：';
                }else{
                    $msg = '已隐藏';
                    $log_msg = '隐藏公司图片：';
                }
                insert_system_log($log_msg);
                return json(['code' => 'suc','msg' => $msg]);
            }
            return json(['code' => 'err','msg' => '系统繁忙，请重试']);
        }
    }

    /**
     * 公司历史
     * @param int $page
     * @param int $limit
     * @return mixed|\think\response\Json
     */
    public function history($page = 1, $limit = 10){
        if (is_post()){
            $list = db('company_history')
                ->order('sort','asc')
                ->page($page,$limit)
                ->select();
            $count = db('company_history')->count();
            return json(['code'=>'suc','msg'=>'数据请求成功','count'=>$count,'data'=>$list]);
        }
        return $this->fetch('company/history/list');
    }

    /**
     * 新增公司历史
     * @return mixed|\think\response\Json
     */
    public function addHistory(){
        if (is_post()){
            $model = new CompanyHistory();

            $result = $model->addHistory(input('post.'));
            return json(['code' => $result['code'], 'msg' => $result['msg']]);
        }
        return $this->fetch('company/history/add');
    }

    /**
     * 编辑公司历史
     * @param $id
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editHistory($id){
        if (is_post()){
            $model = new CompanyHistory();
            $result = $model->editHistory(input('post.'),$id);

            return json(['code' => $result['code'], 'msg' => $result['msg']]);
        }
        $info = db('company_history')
            ->where('id',$id)
            ->find();
        $this->assign('info',$info);
        return $this->fetch('company/history/edit');
    }

    /**
     * 删除公司历史
     * @param $id
     * @return \think\response\Json
     */
    public function delHistory($id){
        if (is_post()){
            $model = new CompanyHistory();
            $result = $model->delHistory($id);
            return json(['code' => $result['code'], 'msg' => $result['msg']]);
        }
    }

    /**
     * 隐私政策
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function privacyPolicy(){
        if (is_post()){
            $data = input('post.');
            $data['update_time'] = time();
            $result = db('policy')
                ->where('id',1)
                ->update( $data);
            if ($result){
                insert_system_log('修改隐私政策');
                return json(['code' => 'suc', 'msg' => '修改成功']);
            }else {
                return json(['code' => 'err', 'msg' => '系统繁忙，请重试']);
            }
        }
        $info = db('policy')
            ->where('id',1)
            ->find();
        $this->assign('info',$info);
        return $this->fetch();
    }

    /**
     * 服务条款
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function termService(){
        if (is_post()){
            $data = input('post.');
            $data['update_time'] = time();
            $result = db('policy')
                ->where('id',2)
                ->update($data);
            if ($result){
                insert_system_log('修改物流政策');
                return json(['code' => 'suc', 'msg' => '修改成功']);
            }else {
                return json(['code' => 'err', 'msg' => '系统繁忙，请重试']);
            }
        }
        $info = db('policy')
            ->where('id',2)
            ->find();
        $this->assign('info',$info);
        return $this->fetch();
    }

    /**
     * 获取公司图片分类
     * @return array
     * @throws
     */
    private function getCompanyPicCategory(){
        $list = db('category_company_picture')
            ->where('is_del',0)
            ->where('status',1)
            ->order('sort','asc')
            ->select();

        return !empty($list) ? $list : [];
    }

    /**
     * 文件上传接口
     * @return \think\response\Json
     */
    public function upload(){
        $file = request()->file('file');
        $info = $this->uploadsImg($file,'uploads/company/');
        if ($info !== false){
            return json(['code' => 'suc','msg' => '图片上传成功','url' => $info['path']]);
        }else{
            return json(['code' => 'err','msg' => '图片上传失败，请重试']);
        }
    }
}